﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace TSharp.Core.Mvc
{
    public static class WebUtil
    {
        /// <summary>
        /// Gets the cookies json.
        /// </summary>
        /// <returns>String.</returns>
        public static String GetCookiesJson()
        {
            var str = string.Empty;
            if (HttpContext.Current != null)
            {
                str = string.Join(",", HttpContext.Current.Request.Cookies.Cast<string>().Select(x =>
                                                                                     {
                                                                                         var httpCookie
                                                                                             = HttpContext.Current.Request.Cookies[x];
                                                                                         return httpCookie != null ? string.Format("'{0}':'{1}'", x, httpCookie.Value) : string.Empty;
                                                                                     }));
                return str;
            }
           
            return str;
        }

        /// <summary>
        /// Determines whether [is jquery ajax].
        /// </summary>
        /// <returns><c>true</c> if [is jquery ajax]; otherwise, <c>false</c>.</returns>
        public static bool IsJQueryAjax()
        {
            if (HttpContext.Current != null)
                return IsJQueryAjax(HttpContext.Current.Request);
            return false;
        }
        /// <summary>
        /// Determines whether [is jquery ajax] [the specified context].
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns><c>true</c> if [is jquery ajax] [the specified context]; otherwise, <c>false</c>.</returns>
        public static bool IsJQueryAjax(ControllerContext context)
        {
            if (context != null)
                return IsJQueryAjax(context.HttpContext.Request);
            return false;
        }

        /// <summary>
        /// Determines whether [is jquery ajax] [the specified request].
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns><c>true</c> if [is jquery ajax] [the specified request]; otherwise, <c>false</c>.</returns>
        public static bool IsJQueryAjax(HttpRequest request)
        {
            if (request.Headers.Get("X-Requested-With") == "XMLHttpRequest")
                return true;
            return false;
        }
        /// <summary>
        /// Determines whether [is jquery ajax] [the specified request].
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns><c>true</c> if [is jquery ajax] [the specified request]; otherwise, <c>false</c>.</returns>
        public static bool IsJQueryAjax(HttpRequestBase request)
        {
            if (request.Headers.Get("X-Requested-With") == "XMLHttpRequest")
                return true;
            return false;
        }
    }
}
